package com.mouse.magent.server.handler;

import com.mouse.magent.common.handler.BaseHandler;
import com.mouse.magent.common.util.NettyUtil;
import com.mouse.magent.server.constant.ServerConstant;
import io.netty.channel.Channel;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelId;
import lombok.extern.slf4j.Slf4j;

import java.util.Map;

@Slf4j
public class ServerHandler extends BaseHandler {
    @Override
    public void channelInactive(ChannelHandlerContext ctx) throws Exception {
        ChannelId channelId = ctx.channel().id();
        Map<ChannelId, Channel> channelRemoteMap = ServerConstant.getChannelRemoteMap();
        Channel channel = channelRemoteMap.get(channelId);
        if (ServerConstant.getProxyChannelGroup().find(channelId) == null) {
            if (channel != null) {
                log.info("Proxy关闭,关闭本地代理port:{}", NettyUtil.getLocalPort(channel));
                channel.close();
            }
            channelRemoteMap.remove(channelId);
        } else if (channel == null) {
            channelRemoteMap.remove(channelId);
        }
    }
}
